﻿using Microsoft.Win32;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ReStartUnityTools : MonoBehaviour
{
	public static string UnityExePath = "E:\\Unity 5.5.2\\Unity\\Editor\\Unity.exe";

	[MenuItem("Tools/ReStartUnity(重启Unity)")]
	public static void ReStartUnity()
	{
		string unityPath = UnityExePath;// GetUnityPath();

		StartPeocess(unityPath);
		string[] args = unityPath.Split('\\');
		Process[] pro = Process.GetProcessesByName(args[args.Length - 1].Split('.')[0]);//Unity
		foreach (var item in pro)
		{
			UnityEngine.Debug.Log(item.MainModule);
			item.Kill();
		}
	}

	private static string GetUnityPath()
	{
		#region 通过注册便获取Unity安装路径
		var regKey = @"Unity package file\DefaultIcon";
		RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(regKey);
		string pathName = (string)registryKey.GetValue(null);  // "(Default)"  
		if (string.IsNullOrEmpty(pathName))
		{
			return null;
		}
		int index = pathName.LastIndexOf(",");
		if (index != -1)
		{
			var exepath = pathName.Substring(0, index).Replace("\"", string.Empty);
			var binpath = Path.GetDirectoryName(exepath);   //  
			var di = new DirectoryInfo(binpath);
			if (di.Parent != null)
			{
				return di.Parent.FullName;
			}
		}
		return null;   
		#endregion
	}

	static void StartPeocess(string applicationPath)
	{
		Process po = new Process();
		po.StartInfo.FileName = applicationPath;
		po.Start();
	}

	
}
